// Purpose : This page is use for Add and update doctor blog
// Date    : 15 August, 2007
// Author  : Vinayak Singh Rathore

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SiteManager;
using System.Text;
using System.Data.SqlClient;

public partial class Community_AddUpdateBlog : System.Web.UI.Page
{
    //BlogMainTable/BlogGridTable
    Manager mgr = new Manager(true,"ConnectionString");
    string sDescription = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Visible =false;        
        txtDescription.RootPath = Session["strHttp"] + "includes/Editor/";
        if (Request.Form["rte1"] != null)
            sDescription = rteSafe(Request.Form["rte1"]);

        if (!IsPostBack)
        {
            // CODE COMMENTED AS DENTIST WERE NOT ABLE TO ACCESS THIS PAGE
            // DATE 18TH FEB 2008

            //if (Session["userID"] != null || Session["userID"].ToString().Length >= 0)
            //{
            //    int userType = 1;
            //    if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length == 0)))
            //        userType = 2;
            //    else if (Session["userType"].ToString() == "SiteUser")
            //        userType = 3;
            //    else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            //        userType = 4;


            //    if (userType != 2 || userType != 4)
            //    {
            //        localFunction lfunction = new localFunction();
            //        lfunction.checkDoctorLogin(true);
            //    }
            //}
            //else
            //{
            //    LoadBlog();
            //}
            //LoadBlog();

            localFunction lfunction = new localFunction();
            if (Session["DoctorType"].ToString() == "Doctor")
                lfunction.checkDoctorLogin(true);
            else if (Session["DoctorType"].ToString() == "Dentist")
                lfunction.checkDentistLogin(true);
            else
                Response.Redirect(Session["strHttp"].ToString());

            LoadBlog();            
        }
       
    }
    string rteSafe(string str)
    {
        string tmpString = str;
        tmpString = tmpString.Replace(Convert.ToChar(145), Convert.ToChar(39));
        tmpString = tmpString.Replace(Convert.ToChar(146), Convert.ToChar(39));
        tmpString = tmpString.Replace("'", "&#39;");
        //	'convert all types of double quotes
        tmpString = tmpString.Replace(Convert.ToChar(147), Convert.ToChar(34));
        tmpString = tmpString.Replace(Convert.ToChar(148), Convert.ToChar(34));
        //	'replace carriage returns & line feeds
        tmpString = tmpString.Replace(Convert.ToChar(10), Convert.ToChar(" "));
        tmpString = tmpString.Replace(Convert.ToChar(13), Convert.ToChar(" "));
        return tmpString;
    }
    protected void btnSubmit_Click1(object sender, EventArgs e)
    {
        if (txtBlogTitle.Text.ToString() == "")
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Write blog Title";
            return;
        }
        if (txtDescription.EditorContent.ToString() == "")
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Write into  Blog message";
            return;
        }
       Manager objManger = new Manager(true,"ConnectionString");
        int userType = 0;  //WILL USE TO SET ZERO(0) FOR DOCTOR AND 1 FOR DENTIST
      //  if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"]).Length == 0)))
        if ((Session["userType"].ToString() == "Doctor") && ((Convert.ToString(Session["DoctorType"])) == "Doctor"))
            userType = 2;
             //userType = 0; Change at 27/02/2008 because at pageresponse.aspx page  doctor usertype=2
        else if (Session["userType"].ToString() == "SiteUser")
            userType = 3;              
        else if ((Session["userType"].ToString() == "Doctor") && (Convert.ToString(Session["DoctorType"]) == "Dentist"))
            userType = 4;
        //userType = 1; Change at 27/02/2008 because at pageresponse.aspx page  dentist usertype=4
        SqlParameter[] objSqlParameter = new SqlParameter[7];
        objSqlParameter[0] = new SqlParameter("@Id",SqlDbType.BigInt);
        //ADD BY RANJEET TO PASS BLOG ID AT 20/02/2008
        //objSqlParameter[0].Value = 0;
        if (btnSubmit.Text.ToUpper().Trim() == "Update".ToUpper().Trim())
        {
            objSqlParameter[0].Value = Convert.ToInt32(Session["EditID"].ToString());
        }
        else
        {
            objSqlParameter[0].Value = 0;
        }
        
        objSqlParameter[1] = new SqlParameter("@DoctorID", SqlDbType.BigInt);
        objSqlParameter[1].Value = Convert.ToInt32(Session["userID"]);
        objSqlParameter[2] = new SqlParameter("@DoctorType", SqlDbType.BigInt);
        objSqlParameter[2].Value = userType;
        objSqlParameter[3] = new SqlParameter("@BlogMessage", SqlDbType.Text);
        //objSqlParameter[3].Value = txtDescription.Text;
        objSqlParameter[3].Value = txtDescription.EditorContent;
        objSqlParameter[4] = new SqlParameter("@IsAddOrUpdate", SqlDbType.Bit);
        objSqlParameter[4].Direction =ParameterDirection.Output;
        objSqlParameter[5] = new SqlParameter("@BlogTitle", SqlDbType.Text);
        objSqlParameter[5].Value = txtBlogTitle.Text.ToString().Trim();
        //ADDED AT 21/02/2008 TO INSERT BLOG POSTED DATE
        objSqlParameter[6] = new SqlParameter("@blogPostedDate", SqlDbType.DateTime);
        objSqlParameter[6].Value = System.DateTime.Now;

        objManger.ExecuteQuery("AddandUpdateBlog", objSqlParameter);
        //COMMENTED BY RANEJET AT 20/02/2008
        //if (Convert.ToBoolean(objSqlParameter[0].Value))        
        //{
        //    lblMessage.Text = "Blog Inserted successfully.";
        //}
        //else
        //{
        //    lblMessage.Text = "Blog Updated Successfully.";
        //}
        if (Convert.ToInt32(objSqlParameter[0].Value)==0)
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Blog Inserted successfully.";
            lblMessage.Font.Bold = true;
            clear_blockEditor();
        }
        else
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Blog Updated Successfully.";
            lblMessage.Font.Bold = true;
            clear_blockEditor();
        }
                               
        objManger = null;
        //Add by Ranjeet at 20/02/2008

        //BlogMainTable.Visible = false;
        //BlogGridTable.Visible = true;
        btnSubmit.Text = "Post";
        LoadBlog();

    }
    /// <summary>
    /// This function will return Message Posted by a doctor
    /// </summary>
    public void LoadBlog()
    {
        Manager objManager = new Manager(true,"ConnectionString");
        SqlDataReader objDataReader = null;
        objDataReader = objManager.ExecuteQuery("select * from blog where DoctorId=" + Convert.ToInt32(Session["userID"]));
        //COMMENTED AT 20/02/2008 BY RANJEET FOR DISPLAY RECORDS INTO GRIDVIEW
//        objDataReader.Read();
//        if (objDataReader.HasRows)
//        {
////            txtDescription.Text = objDataReader["BlogMessage"].ToString();
//            txtDescription.EditorContent = objDataReader["BlogMessage"].ToString();
//            txtBlogTitle.Text = objDataReader["BlogTitle"].ToString();
//        }
        //ADD CODE TO FILL GRIDVIEW

        //BlogMainTable.Visible = false;
        //BlogGridTable.Visible = true;

        BindGrid(Convert.ToInt32(Session["userID"]));
        objManager = null;
    }

    //ADD CODE TO DISPLAY/EDIT/VIEW ALL BLOG OF DOCTORS
    private void BindGrid(int id)
    {
        //Filling the grid with the messages of the topic selected in the combo
        Manager mgrobj = new Manager(true,"ConnectionString");
        string sql = "select ID,blogTitle from blog where DoctorId=" + Convert.ToInt32(Session["userID"]) + "";
        DataSet ds = mgrobj.ExecuteQuery(sql, "tbl");        
        if (ds.Tables[0].Rows.Count > 0)
        {
            grdMessages.DataSource = ds.Tables[0];
            //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
            grdMessages.DataBind();
        }
        else // no topics exist
        {
            grdMessages.DataSource = ds.Tables[0];
            //grdMessages.PageSize = Convert.ToInt32(Session["AdminPageSize"]);
            grdMessages.DataBind();
            lblMessage.Text = "No Blog exist for you!!";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            return;
            
        }
        mgrobj = null;
    }

    protected void grdMessages_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdMessages.PageIndex = e.NewPageIndex;
        BindGrid(Convert.ToInt32(Session["userID"]));
    }

    // delete confirmation
    protected void grdMessages_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lnk = (LinkButton)e.Row.FindControl("lnkDelete");

                try
                {
                    string id = grdMessages.DataKeys[e.Row.RowIndex].Value.ToString();
                    lnk.Attributes.Add("onclick", "return confirm('Are you sure to delete this blog?')");
                }
                catch (Exception es)
                {
                    Response.Write(es.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    // delete the topic
    protected void grdMessages_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            string id = grdMessages.DataKeys[e.RowIndex].Value.ToString();
            string delSql = "delete from blog where doctorID=" + Convert.ToInt32(Session["userID"]) + " and id=" + id + "";
            mgr.ExecuteQuery(delSql);            
            BindGrid(Convert.ToInt32(Session["userID"]));
            btnSubmit.Text = "Post";
            clear_blockEditor();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    protected void grdMessages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToUpper().Trim() == "Editblog".ToUpper().Trim())
        {

            Manager objManager = new Manager(true,"ConnectionString");
            SqlDataReader objDataReader = null;
            //string editSql = "update blog set BlogMessage='nnnnnnnn', BlogTitle='mmmmmm' where doctorID=759462 and id=6";
            string id = e.CommandArgument.ToString();
            Session["EditID"] = id;
            string editsql = "select * from blog where doctorID=" + Convert.ToInt32(Session["userID"]) + " and id=" + id + "";
            objDataReader = objManager.ExecuteQuery(editsql);
            objDataReader.Read();
            if (objDataReader.HasRows)
            {
                //            txtDescription.Text = objDataReader["BlogMessage"].ToString();
                txtDescription.EditorContent = objDataReader["BlogMessage"].ToString();
                txtBlogTitle.Text = objDataReader["BlogTitle"].ToString();
                //SET VISIBILITY
                //BlogMainTable.Visible = true;
                //BlogGridTable.Visible = false;
                 lblMessage.Text = "";                
                 btnSubmit.Enabled = true;
                 btnSubmit.Text = "Update";                
            }
            objDataReader.Close();
            objManager = null;
        }                
    }

    protected void lnkNewEntry_Click(object sender, EventArgs e)
    {
        btnSubmit.Text = "Post";
        clear_blockEditor();
    }
    protected void clear_blockEditor()
    {
        txtDescription.EditorContent = "";
        txtBlogTitle.Text = "";
    }
}
